package com.ruoyi.scene.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.scene.domain.bo.SceneInspectionIssueBo;
import com.ruoyi.scene.service.ISceneInspectionIssueService;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.scene.domain.bo.SceneInspectionIssueRecordingBo;
import com.ruoyi.scene.domain.vo.SceneInspectionIssueRecordingVo;
import com.ruoyi.scene.domain.SceneInspectionIssueRecording;
import com.ruoyi.scene.mapper.SceneInspectionIssueRecordingMapper;
import com.ruoyi.scene.service.ISceneInspectionIssueRecordingService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 现场巡查问题记录Service业务层处理
 *
 * @author llb
 * @date 2024-10-16
 */
@RequiredArgsConstructor
@Service
public class SceneInspectionIssueRecordingServiceImpl implements ISceneInspectionIssueRecordingService {

    private final SceneInspectionIssueRecordingMapper baseMapper;

    private final SysUserMapper sysUserMapper;

    private final ISceneInspectionIssueService iSceneInspectionIssueService;

    private final ISysOssService sysOssService;

    /**
     * 查询现场巡查问题记录
     */
    @Override
    public SceneInspectionIssueRecordingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询现场巡查问题记录列表
     */
    @Override
    public TableDataInfo<SceneInspectionIssueRecordingVo> queryPageList(SceneInspectionIssueRecordingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SceneInspectionIssueRecording> lqw = buildQueryWrapper(bo);
        Page<SceneInspectionIssueRecordingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询现场巡查问题记录列表
     */
    @Override
    public List<SceneInspectionIssueRecordingVo> queryList(SceneInspectionIssueRecordingBo bo) {
        LambdaQueryWrapper<SceneInspectionIssueRecording> lqw = buildQueryWrapper(bo);
        List<SceneInspectionIssueRecordingVo> sceneInspectionIssueRecordingVos = baseMapper.selectVoList(lqw);
        sceneInspectionIssueRecordingVos.forEach(t->{

            //整改人或复查人
            SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(t.getPersonnel()));
            if (ObjectUtils.isNotEmpty(sysUser)){
                t.setPersonnelName(sysUser.getNickName());
            }

            String fileId = t.getFileId();
            if (ObjectUtils.isNotEmpty(fileId)){
                String[] split = fileId.split(",");
                Long[] fileIds =(Long[]) ConvertUtils.convert(split,Long.class);
                List<SysOssVo> sysOssVos = sysOssService.listByIds(Arrays.asList(fileIds));
                t.setOssVoList(sysOssVos);
            }
        });
        return sceneInspectionIssueRecordingVos;
    }

    private LambdaQueryWrapper<SceneInspectionIssueRecording> buildQueryWrapper(SceneInspectionIssueRecordingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SceneInspectionIssueRecording> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getInspectionId() != null, SceneInspectionIssueRecording::getInspectionId, bo.getInspectionId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SceneInspectionIssueRecording::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonnel()), SceneInspectionIssueRecording::getPersonnel, bo.getPersonnel());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SceneInspectionIssueRecording::getContent, bo.getContent());
        lqw.eq(bo.getDate() != null, SceneInspectionIssueRecording::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getOutcome()), SceneInspectionIssueRecording::getOutcome, bo.getOutcome());
        lqw.eq(StringUtils.isNotBlank(bo.getFileId()), SceneInspectionIssueRecording::getFileId, bo.getFileId());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovalStatus()), SceneInspectionIssueRecording::getApprovalStatus, bo.getApprovalStatus());
        return lqw;
    }

    /**
     * 新增现场巡查问题记录
     */
    @Override
    public Boolean insertByBo(SceneInspectionIssueRecordingBo bo) {
        SceneInspectionIssueRecording add = BeanUtil.toBean(bo, SceneInspectionIssueRecording.class);
        if ("1".equals(add.getType())){
            SceneInspectionIssueBo issueBo = new SceneInspectionIssueBo();
            issueBo.setId(bo.getInspectionId());
            issueBo.setApprovalStatus("22");
            iSceneInspectionIssueService.submit(issueBo);
        }
        if ("2".equals(add.getType())){
            SceneInspectionIssueBo issueBo = new SceneInspectionIssueBo();
            issueBo.setId(bo.getInspectionId());
            issueBo.setApprovalStatus(add.getApprovalStatus());
            iSceneInspectionIssueService.submit(issueBo);
        }

        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改现场巡查问题记录
     */
    @Override
    public Boolean updateByBo(SceneInspectionIssueRecordingBo bo) {
        SceneInspectionIssueRecording update = BeanUtil.toBean(bo, SceneInspectionIssueRecording.class);
        validEntityBeforeSave(update);
        //先删除本次修改删掉的附件；
        Set<Long> fileNoOld = bo.getFileNoOld();
        Set<Long> fileNos = bo.getFileNos();
        Set<Long> deleteFiles;
        if(CollUtil.isNotEmpty(fileNoOld)){
            if(CollUtil.isEmpty(fileNos)){
                deleteFiles = fileNoOld;
            } else {
                deleteFiles = fileNoOld.stream().filter(e-> !fileNos.contains(e)).collect(Collectors.toSet());
            }
            if(CollUtil.isNotEmpty(deleteFiles)){
                sysOssService.deleteWithValidByIds(deleteFiles,true);
            }
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SceneInspectionIssueRecording entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除现场巡查问题记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
